package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Book;
import  dao.BookDAO;
import entity.Page;

/**
 * Servlet implementation class BookAddServlet
 */
@WebServlet("/addBook")
public class BookAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	BookDAO bookDao = new BookDAO();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BookAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Book book = new Book();
		book.setBookName(request.getParameter("bookName"));
		book.setPicUrl(request.getParameter("picUrl"));
		book.setPrice(Float.parseFloat(request.getParameter("price")));
		String mes = "";
		if(bookDao.getDoubleName(book.getBookName())==0) {
		if(bookDao.add(book) > 0){
			mes = "添加成功！";
		} else {
			mes = "添加失败!";
		}
		}else {
			mes = "命名重复!";
		}
		request.setAttribute("MES", mes);
		request.getRequestDispatcher("BookShow").forward(request, response);	
	}

}
